package io.geekidea.boot.wltype.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.boot.framework.response.ApiCode;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.wltype.dto.WlTypeAddDto;
import io.geekidea.boot.wltype.dto.WlTypeBanDto;
import io.geekidea.boot.wltype.entity.WlType;
import io.geekidea.boot.wltype.query.WlTypeQuery;
import io.geekidea.boot.wltype.service.IWlTypeService;
import io.geekidea.boot.wlwarehouse.dto.WlWareHouseUpdateDto;
import io.geekidea.boot.wlwarehouse.entity.WlWarehouse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.BeanUtils;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 物料类型 前端控制器
 * </p>
 *
 * @author xukang
 * @since 2023-11-20
 */
@RestController
@RequestMapping("/wltype")
@Tag(name="物料类型接口")
public class WlTypeController {

    @Resource
    private IWlTypeService wlTypeService;



    @PostMapping("/findAll")
    @Operation(summary = "物料类型分页查询")
    public ApiResult findPage(@RequestBody WlTypeQuery wlTypeQuery){
        Page<WlType> page=new Page<>(wlTypeQuery.getPage(),wlTypeQuery.getSize());
       Page<WlType> pageDto=wlTypeService.page(page) ;
       return ApiResult.success(pageDto);
    }



    @PostMapping("/update")
    @Operation(summary = "编辑物料类型")
    public ApiResult ban(@RequestBody WlTypeBanDto wlTypeBanDto){
        WlType wlType = new WlType();
        BeanUtils.copyProperties(wlTypeBanDto,wlType);
        boolean result = wlTypeService.updateById(wlType);
        return result?ApiResult.success("编辑物料类型成功",200):ApiResult.fail("操作失败");
    }

    @PostMapping("/add")
    @Operation(summary = "新增一个物料类型")
    public ApiResult addWarehouse(@RequestBody WlTypeAddDto wlTypeAddDto){
        if(StringUtils.isBlank(wlTypeAddDto.getName())) return ApiResult.fail("不可添加物料类型");
        WlType wlType = new WlType();
        BeanUtils.copyProperties(wlTypeAddDto,wlType);
        boolean save = wlTypeService.save(wlType);
        return save?ApiResult.success("添加成功",200):ApiResult.fail("添加失败");
    }

    @GetMapping("/name")
    public ApiResult getAllSuppliers(){
        List<WlType> list=wlTypeService.list();

        return ApiResult.result(ApiCode.SUCCESS,list);
    }
    @GetMapping("/getOneType/{id}")
    public ApiResult getOneType(Integer id){
        final  WlType wlType=wlTypeService.getById(id);
        return ApiResult.success(wlType);
    }

    @PostMapping("/list")
    public ApiResult list(){
        List<WlType> list=wlTypeService.list();
        return ApiResult.success(list);
    }


}
